package com.example.wangandroid.activity

import android.os.Bundle
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.LogUtils
import com.example.activity.BaseActivity
import com.example.wangandroid.adapter.TreeAdapterList
import com.example.wangandroid.data.model.ListAricleViewModel
import com.example.wangandroid.databinding.ActivityListarticleBinding

@Route(path = "/app/listArticle")
class ListArticleActivity: BaseActivity() {
    @JvmField
    @Autowired(name = "id")
    public var id:Int = 0
    @JvmField
    @Autowired(name = "cid")
    public var cid:Int = 0

    lateinit var binding:ActivityListarticleBinding
    lateinit var viewModel: ListAricleViewModel
    var treeAdapter:TreeAdapterList? = null
    lateinit var recylist:RecyclerView
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityListarticleBinding.inflate(layoutInflater)
        setContentView(binding.root)
        ARouter.getInstance().inject(this)
        binding.ivBack.setOnClickListener {
            finish()
        }
        viewModel = ViewModelProvider.NewInstanceFactory().create(ListAricleViewModel::class.java)
        viewModel.AricleData.observe(this, Observer {
            if(treeAdapter==null){
                LogUtils.d(it.data.datas.get(0).chapterName)
                treeAdapter = it?.let { it1 -> TreeAdapterList(it1) }
                binding.rv.adapter = treeAdapter
            }else{
                treeAdapter!!.setData(it)
                treeAdapter!!.notifyDataSetChanged()
            }
        })
            //    viewModel.getArticleList(id,cid)
    }
}